package com.russell.pms.dao.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.russell.pms.dao.MaintenanceDao;
import com.russell.pms.model.Maintenance;

public class MaintenanceDaoJDBC extends BaseMysqlDaoJDBC implements MaintenanceDao {

	private static final class MaintenanceRowMapper implements RowMapper<Maintenance> {

		@Override
		public Maintenance mapRow(ResultSet rs, int rowNum) throws SQLException {
			Maintenance maintenance = new Maintenance();
			maintenance.setId(rs.getInt("MAINT_ID"));
			maintenance.setDescription(rs.getString("DESCRIPTION"));
			maintenance.setProp_id(rs.getInt("PROP_ID"));
			maintenance.setVendor_id(rs.getInt("VENDOR_ID"));
			maintenance.setAmount(rs.getBigDecimal("AMOUNT"));
			maintenance.setDate(rs.getDate("DATE"));

			maintenance.setDateModified(rs.getDate("MODIFIED_DATE"));
			maintenance.setDateCreated(rs.getDate("CREATED_DATE"));
			maintenance.setModifiedBy(rs.getString("MODIFIED_BY"));

			return maintenance;
		}

	}
	
	@Override
	public List<Maintenance> getProperties() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Maintenance getMaintenance(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removeMaintenance(Maintenance maintenance) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void saveMaintenance(final Maintenance maintenance) {
		final StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO pms_app.MAINTENANCE");
		sql.append(" (DESCRIPTION, PROP_ID, VENDOR_ID, AMOUNT, DATE,");
		sql.append(" CREATED_BY, CREATED_DATE, MODIFIED_BY, MODIFIED_DATE)");
		sql.append(" VALUES(?,?,?,?,?,?,?,?,?)");

		KeyHolder keyHolder = new GeneratedKeyHolder();

		getJdbcTemplate().update(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(
					Connection connection) throws SQLException {
				PreparedStatement ps = connection.prepareStatement(
						sql.toString(), new String[] { "maint_id" });
				ps.setString(1, maintenance.getDescription());
				ps.setInt(2, maintenance.getProp_id());
				ps.setInt(3, maintenance.getVendor_id());
				ps.setBigDecimal(4, maintenance.getAmount());
				Timestamp dateEntered = null;
				if (maintenance.getDate() != null){
					dateEntered = new Timestamp(maintenance.getDate()
							.getTime());
				}
				ps.setTimestamp(5, dateEntered);
				ps.setString(6, maintenance.getCreatedBy());
				Timestamp dateCreated = null;
				if (maintenance.getDateCreated() != null) {
					dateCreated = new Timestamp(maintenance.getDateCreated()
							.getTime());
				}
				ps.setTimestamp(7, dateCreated);
				ps.setString(8, maintenance.getModifiedBy());
				Timestamp dateModified = null;
				if (maintenance.getDateCreated() != null) {
					dateModified = new Timestamp(maintenance.getDateCreated()
							.getTime());
				}
				ps.setTimestamp(9, dateModified);
				return ps;
			}

		}, keyHolder);

		maintenance.setId(keyHolder.getKey().intValue());
	}

	@Override
	public void updateMaintenance(Maintenance maintenance) {
		// TODO Auto-generated method stub
		
	}

	
}
